/* registration validation ----------------------------------------------- */
//component validity
var valid=true;
var username_valid=true;
var password_valid=true;
var confirm_valid=true;
var fullname_valid=true;
var birthdate_valid=true;
var email_valid=true;
var sex_valid=true;
var avatar_valid=true;

function check_validity(){
	//get all form elements
	var form = document.getElementById("reg_form");
	var username = form.elements["username"].value;
	var password = form.elements["password"].value;
	var confirm = form.elements["confirm"].value;
	var fullname = form.elements["fullname"].value;
	var birthdate = form.elements["birthdate"].value;
	var email = form.elements["email"].value;
	var sexmale = document.getElementById("male");
	var sexfemale = document.getElementById("female");
	var avatar = form.elements["avatar"].value;	
		
	//validate username
	username_valid = username.length >= 5 && username != password;
	if(!username_valid){
		document.getElementById("error_username").style.display="block";
	} else {
		document.getElementById("error_username").style.display="none";
	}
	
	//validate password
	password_valid = password.length >=8 && password != username && password != email;
	if(!password_valid){
		document.getElementById("error_password").style.display="block";
	} else {
		document.getElementById("error_password").style.display="none";
	}
	
	//validate confirm
	confirm_valid = confirm == password;
	if(!confirm_valid){
		document.getElementById("error_confirm").style.display="block";
	} else {
		document.getElementById("error_confirm").style.display="none";
	}
	
	//validate fullname
	var regex = new RegExp("^[a-zA-Z]+ +[a-zA-Z]+$");
	fullname_valid = regex.test(fullname);
	if(!fullname_valid){
		document.getElementById("error_fullname").style.display="block";
	} else {
		document.getElementById("error_fullname").style.display="none";
	}
	
	//validate birthdate
	regex = new RegExp("^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
	birthdate_valid = regex.test(birthdate);
	if(!birthdate_valid){
		document.getElementById("error_birthdate").style.display="block";
	} else {
		document.getElementById("error_birthdate").style.display="none";
	}
	
	//validate email
	regex = new RegExp("^[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+[a-zA-Z0-9]+$");
	email_valid = email != password && regex.test(email);
	if(!email_valid){
		document.getElementById("error_email").style.display="block";
	} else {
		document.getElementById("error_email").style.display="none";
	}
	
	//validate avatar
	regex = new RegExp("[jpg|jpeg]$");
	avatar_valid = regex.test(avatar);
	if(!avatar_valid){
		document.getElementById("error_avatar").style.display="block";
	} else {
		document.getElementById("error_avatar").style.display="none";
	}
	
	//validate sex
	sex_valid = sexmale.checked || sexfemale.checked;
	if(!sex_valid){
		document.getElementById("error_sex").style.display="block";
	} else {
		document.getElementById("error_sex").style.display="none";
	}
	
	valid = username_valid && password_valid && confirm_valid && fullname_valid && birthdate_valid
			&& email_valid && sex_valid && avatar_valid;
	if(valid){
		document.getElementById("submit_button").disabled=false;
	} else {
		document.getElementById("submit_button").disabled=true;
	}
	
	set_cookie("coba", 80, 10);
}

/* change theme ---------------------------------------------------------- */

function switch_style ( css_title )
{
	var i, link_tag ;
	for (i = 0, link_tag = document.getElementsByTagName("link"); i < link_tag.length ; i++ ) {
		if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) && link_tag[i].title) {
			link_tag[i].disabled = true ;
			if (link_tag[i].title == css_title) {
				link_tag[i].disabled = false ;
			}
		}
	}
}